<?php
namespace app\admin\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;

class Login extends BaseController
{
    /**
     * 管理后台登录
     * @return string|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        if(Request::method()=='POST'){  //点击登录按钮，查询
            $all = Request::param();
            $admin = Db::table('shop_admin')->where('account',$all['account'])->find();
            if(empty($admin)){  //查询不到记录
                echo json_encode(['code'=>1,'msg'=>'未找到该管理员']);
                exit;
            }
            if(md5($all['pwd'])!=$admin['password']){  //md5加密比较
                echo json_encode(['code'=>1,'msg'=>'密码错误']);
                exit;
            }
            Session::set('uid',$admin['uid']);
            Session::set('account',$admin['account']);
            echo json_encode(['code'=>0,'msg'=>'登录成功']);
        }else{  //首次加载
            $title = '商城';
            MyViewAssign('title',$title);
            return MyView();
        }
    }

    /**
     * 退出登录
     * @return \think\response\Redirect
     */
    public function loginOut()
    {
        session_unset();
        session(null);
        Session::clear();
        return redirect('/admin/login');
    }
}